package patchos

import (
	"log"
	"runtime"
)

type osType int

const (
	WindowsType osType = iota + 1
	MacOSType
	LinuxType
)

type Os interface {
	// GetOsType 获取当前操作系统类型
	GetOsType() osType
	// OpenFolder 打开对应文件夹
	OpenFolder(dirPath string)
}

var osImplArr = make([]Os, 0)

// 注册所有实现类
func init() {
	osImplArr = append(osImplArr, &Windows{})
	osImplArr = append(osImplArr, &Linux{})
	osImplArr = append(osImplArr, &MacOs{})
}

// 获取当前操作系统实现类
func getCurrentOsImpl() Os {
	goos := runtime.GOOS
	var currentOsType osType
	log.Printf("当前操作系统: %s\n", goos)
	switch goos {
	case "windows":
		currentOsType = WindowsType
	case "linux":
		currentOsType = LinuxType
	}

	for _, osImpl := range osImplArr {
		if osImpl.GetOsType() == currentOsType {
			return osImpl
		}
	}

	panic("未获取到当前操作系统的实现类, goos: " + goos)
}

// OpenOsFolder 打开目录
func OpenOsFolder(dirPath string) {
	osImpl := getCurrentOsImpl()
	osImpl.OpenFolder(dirPath)
}
